package com.itic.hpp.carrage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itic.appbase.framework.Base.BaseCode;
import com.itic.system.carrage.template.persistence.model.CarrageTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mobile.base.BaseMobile;
import com.itic.system.carrage.template.persistence.model.CarrageTemplateExample;
import com.itic.system.carrage.template.service.CarrageTemplateService;
import com.itic.system.carrage.templatePiece.service.CarrageTemplatePieceService;
import com.itic.system.carrage.templateRegion.service.CarrageTemplateRegionService;
import com.itic.system.user.user.persistence.model.HppUser;
import com.itic.system.user.user.service.HppUserService;
import com.itic.system.vo.CarrageAddVo;
import com.itic.system.vo.CarrageEditVo;
import com.itic.system.vo.CarrageEditWholeVo;
import com.itic.system.vo.CarrageVo;
import com.itic.system.vo.RegionProvinceVo;
import com.itic.system.vo.TemplateContVo;
import com.itic.system.vo.UserVo;
import com.itic.system.region.service.HppRegionService;
@Controller
@RequestMapping(value = "/hpp/carrage")
public class CarageInterface extends BaseMobile {
	@Autowired
	private HppRegionService hppRegionService;
	@Autowired
	private HppUserService hppUserService;
	@Autowired
	private CarrageTemplateService carrageTemplateService;
	@Autowired
	private CarrageTemplatePieceService carrageTemplatePieceService;
	@Autowired
	private CarrageTemplateRegionService carrageTemplateRegionService;
	// 返回属性黑名单
	private static String[] ignoreProperties = new String[] { "createUser", "isEffective", "updateTime", "orgCode","note" };

	@RequestMapping(value = "/param")
	@ResponseBody
	public Map<String, Object> param(String templateId,HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		String result = ResponseCode.SERVER_ERROR.getCode();
		try {
			List<TemplateContVo> volist = new ArrayList<TemplateContVo>();
			String[] ids = { "859967134866866180", "859967134816534528", "859967134816534530" };
			TemplateContVo vo = new TemplateContVo();
			vo.setAddPcount(1);
			vo.setAddTeranfee(10.0);
			vo.setFirstPcount(1);
			vo.setFirstTranfee(5.0);
			vo.setIsDefault("0");
			vo.setRegionIds(ids);
			volist.add(vo);
			TemplateContVo voa = new TemplateContVo();
			voa.setAddPcount(1);
			voa.setAddTeranfee(10.0);
			voa.setFirstPcount(1);
			voa.setFirstTranfee(5.0);
			voa.setIsDefault("0");
			voa.setRegionIds(ids);
			volist.add(vo);
			CarrageAddVo vos = new CarrageAddVo();
/*			vos.setOpenId("1");
*/			vos.setName("模板2");
			vos.setVolist(volist);
			this.add(vos, request, response);
			resultMap.put(DATA, vos);
			carrageTemplateService.get(templateId);
			carrageTemplatePieceService.getBytemplateId(templateId);
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("UserInterface", "add", e.toString(), "添加运费模板", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}

	/**
	 * 添加运费模板
	 *
	 * @param name
	 * @param vo
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Map<String, Object> add(@RequestBody CarrageAddVo vos, HttpServletRequest request,
								   HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		String result = ResponseCode.SERVER_ERROR.getCode();
		try {
			String userId = vos.getUserId();
			String name = vos.getName();
			List<TemplateContVo> volist = vos.getVolist();
			if (StringHelper.isNotBlank(userId) && StringHelper.isNotBlank(name) && volist.size() > 0) {
				HppUser hppUser = hppUserService.get(userId);
				if (hppUser != null) {
					String templateId = IdGen.uuid();
					result = carrageTemplateService.addCarrageTemplate(userId, name, templateId, volist);
					if (ResponseCode.SUCCESS.getCode().equals(result)) {
						errorCode = SUCCESS_ERROR_CODE;
						errorMsg = SUCCESS_ERROR_MSG;
					}
				} else {
					errorCode = USER_ERROR_CODE;
					errorMsg = USER_ERROR_MSG;
				}
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("UserInterface", "add", e.toString(), "添加运费模板", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}

	/**
	 * 运费模板列表
	 * 
	 * @param userId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(String userId, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
 		String result = ResponseCode.SERVER_ERROR.getCode();
		try {
			HppUser hppUser = hppUserService.get(userId);
			if (hppUser != null) {
				errorCode = SUCCESS_ERROR_CODE;
				errorMsg = SUCCESS_ERROR_MSG;
				CarrageTemplateExample example = new CarrageTemplateExample();
				this.handlePageQueryDefault(request,example);
				List<CarrageVo> list = carrageTemplateService.gettemplateList(example,userId);
				resultMap.put(DATA, list);
			} else {
				errorCode = USER_ERROR_CODE;
				errorMsg = USER_ERROR_MSG;
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("UserInterface", "add", e.toString(), "运费模板列表", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}

	/**
	 * 编辑
	 * @param templateId
	 * @param list
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/edit")
	@ResponseBody
	public Map<String, Object> edit(@RequestBody CarrageEditWholeVo vos, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		String result = ResponseCode.SERVER_ERROR.getCode();
		try {
			if(vos != null){
				String templateId = vos.getTemplateId();
				String userId = vos.getUserId();
				String tempName = vos.getTempName();
				HppUser hppUser = hppUserService.get(userId);
				if(hppUser != null){
					List<CarrageEditVo> list = vos.getList();
					if (StringUtils.isNotBlank(templateId) && list.size() > 0) {
						result = carrageTemplateService.editCarrageTemplate(userId,templateId,tempName,list);
						if (ResponseCode.SUCCESS.getCode().equals(result)) {
							errorCode = SUCCESS_ERROR_CODE;
							errorMsg = SUCCESS_ERROR_MSG;
						}
					}
				}else{
					errorCode = USER_ERROR_CODE;
					errorMsg = USER_ERROR_MSG;
				}
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("UserInterface", "edit", e.toString(), "编辑运费模板列表", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}

	/**
	 * 删除模板
	 * @param openId
	 * @param templateId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/del")
	@ResponseBody
	public Map<String, Object> del(String userId,String templateId,HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			HppUser hppUser = hppUserService.get(userId);
			if (hppUser != null) {
				Boolean isOwn = carrageTemplateService.isOwn(userId,templateId);
				if(isOwn){
					carrageTemplateService.delCarrageTemplate(userId, templateId);
					errorCode = SUCCESS_ERROR_CODE;
					errorMsg = SUCCESS_ERROR_MSG;
				}else{
					errorCode = USER_AUTH_ERROR_CODE;
					errorMsg = USER_AUTH_ERROR_MSG;
				}
			} else {
				errorCode = USER_ERROR_CODE;
				errorMsg = USER_ERROR_MSG;
			}
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("UserInterface", "del", e.toString(), "删除模板", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}

	/**
	 * 小程序选择运费模板列表
	 * @param userId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/selectList")
	@ResponseBody
	public Map<String, Object> selectList(String userId, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if(StringUtils.isNotEmpty(userId)){
				List<CarrageTemplate> carrageTemplates = this.carrageTemplateService.getlistByUserId(userId);
				List<Map<String, Object>> list = new ArrayList<>();
				for(CarrageTemplate carrageTemplate : carrageTemplates){
					Map<String, Object> data = new HashMap<String, Object>();
					data.put("carrageTemplateId",carrageTemplate.getId());
					data.put("name",carrageTemplate.getName());
					list.add(data);
				}
				resultMap.put(BaseCode.DATA,list);
				resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
				resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
			}else{
				resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
				resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
			}
		} catch (Exception e) {
			resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
			resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
			logException("UserInterface", "selectList", e.toString(), "小程序选择运费模板列表", e);
		}
		return resultMap;
	}


}
